import { stringify } from 'querystring';
import type { Reducer, Effect } from 'umi';
import { history } from 'umi';

import { loginHandle } from '@/services/login';

// import { message } from 'antd';
import message from '@/components/Message/index';

export type StateType = {
  status?: 'ok' | 'error';
  type?: string;
  currentAuthority?: 'user' | 'guest' | 'admin';
};

export type LoginModelType = {
  namespace: string;
  state: StateType;
  effects: {
    login: Effect;
    logout: Effect;
  };
  reducers: {
    // changeLoginStatus: Reducer<StateType>;
  };
};

const Model: LoginModelType = {
  namespace: 'login',

  state: {
    status: 'error',
  },

  effects: {
    *login({ payload, callback, onerror }, { call, put }) {
      try {
        const response = yield call(loginHandle, payload);
        callback(response);
      } catch (error) {
        console.log(error);
        onerror(error);
      }
    },

    logout() {},
  },

  reducers: {},
};

export default Model;
